<div class="layui-fluid" id="Inventory-edit">
    <div class="layui-card">
        <div class="layui-form" lay-filter="LAY-list-form">
            <div id="view"></div>
        </div>
    </div>
</div>
<script id="page1" type="text/html">
    <div class="layui-card-header">新增盘点单</div>
    <div class="layui-card-body">
        <div class="layui-tab"  lay-filter="test">
            <ul class="layui-tab-title" id="tab_">
                <li class="layui-this">SKU盘点</li>
                <li>全库盘点</li>
            </ul>
            <div class="layui-tab-content">
                <div class="tab1 layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" >选择仓库</label>
                        </div>
                        <div class="layui-inline">
                            <select name="base_warehouse_id_sku" class="selector" lay-search>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">SKU：</label>
                            <div class="layui-input-block">
                                <input type="text" name="sku" id="sku" placeholder="请输入SKU" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <input type="button"  lay-submit lay-filter="LAY-submit-1" value="搜索" class="layui-btn layui-btn-normal LAY-submit1">
                            <button type="reset" class="reset layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                    <div class="layui-item">
                        <input type="button"  lay-submit lay-filter="LAY-submit-2" value="批量删除" class="layui-btn layui-btn-danger LAY-submit2">
                    </div>
                    <table id="LAY-list" lay-filter="LAY-list"></table>
                </div>

                <div class="tab2 layui-tab-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" >选择国家</label>
                    </div>
                    <div class="layui-inline">
                        <select name="base_country_id" lay-filter="base_country_id" class="selector" lay-search>
                        </select>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label" >选择仓库</label>
                    </div>
                    <div class="layui-inline">
                        <select name="base_warehouse_id" class="selector" lay-search>
                        </select>
                    </div>
                    <table id="LAY-list2" lay-filter="LAY-list2"></table>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card-body layui-jy-center">
        <a lay-href="Inventory/getList" class="layui-btn">返回</a>
        <input type="button"  lay-submit lay-filter="LAY-submit-3" value="生成盘点单" class="layui-btn layui-btn-normal LAY-submit3">
    </div>

</script>
<!-- 行内工具 -->
<script type="text/html" id="LAY-list-toolbar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del" href="javascript:;">删除</a>
</script>
<script>
    var ID = "";
    layui.use(['admin', 'form', 'toolJ', 'laytpl', 'upload', 'table', 'element'], function () {
        var $ = layui.$
            , form = layui.form
            , laytpl = layui.laytpl
            , table = layui.table
            , toolJ = layui.toolJ;
        var id = layui.router().search.id;
        var base_warehoue_id_sku = "";
        var sku_ = "";
        var checked_ = [];
        var element = layui.element;
        viewHtml([]);
        var type = layui.router().search.type;
        function tableView(data) {
            var url = '';
            var jdata = [];
            if (data.length > 0) {
                jdata = data;
            }
            table.render({
                elem: '#LAY-list'
                , url: url
                , data: jdata
                , toolbar: true
                , defaultToolbar: ['exports', 'filter', 'print']
                , cellMinWidth: 80
                , cols: [[
                    {type:'checkbox'}
                    , {field: 'sku', title: 'sku'}
                    , {field: 'sku_name', title: 'SKU中文名称'}
                    , {field: 'physical_stock', title: '库存数量'}
                    , {field: 'base_warehouse_name', title: '所属仓库'}
                    , {fixed: 'right', align:'center',title:'操作', toolbar: '#LAY-list-toolbar', width:120}
                ]]
                , page: false
                , limit: 10000
                , text: {none: '暂无相关数据'}
            });
        }

        /*监听行内操作*/
        table.on('tool(LAY-list)', function(obj){
            switch (obj.event) {
                case 'del':
                    layer.confirm('你确定要删除该条数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        var list = [];
                        if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                            $.each(table.cache['LAY-list'],function (index,item) {
                               if (item.id != obj.data.id){
                                   list.push(item);
                               }
                            });
                        }
                        layer.closeAll();
                        tableView(list);
                        table.render(null, 'LAY-list');
                        table.render(null, 'LAY-list2');
                        form.render(null, 'LAY-list-form');
                    });
                    break;
            }
        });

        function viewHtml(data) {
            var getTpl = page1.innerHTML
                , view = $('#view');
            laytpl(getTpl).render(1, function (html) {
                view.html(html);
            });
            var jdata = [];
            if (data.length > 0) {
                jdata = data;
            }
            tableView(jdata);
            var warehouse = layui.data(layui.setter.tableName).warehouse;
            var country = layui.data(layui.setter.tableName).country;
            var selectHtmlCountry = '<option value="">请选择</option>';
            for (var i in country) {
                selectHtmlCountry += '<option value="' + i + '">' + country[i] + '</option>';
            }
            $("select[name='base_country_id']").append(selectHtmlCountry);
            var selectHtmlWarehouse = '<option value="">请选择</option>';
            for (var i_ in warehouse) {
                var selected = "";
                if (base_warehoue_id_sku == i_) selected = "selected";
                selectHtmlWarehouse += '<option value="' + i_ + '" '+selected+'>' + warehouse[i_] + '</option>';
            }
            $("#sku").val(sku_);
            $("select[name='base_warehouse_id_sku']").append(selectHtmlWarehouse);
            table.render(null, 'LAY-list');
            table.render(null, 'LAY-list2');
            form.render(null, 'LAY-list-form');
        }

        //监听搜索
        form.on('submit(LAY-submit-1)', function (data) {
            data.field['type'] = 1;
            if (data.field.base_warehouse_id_sku == "") {
                toolJ.error("请选择仓库");
                return false;
            }
            base_warehoue_id_sku = data.field.base_warehouse_id_sku;
            sku_ = data.field.sku;
            toolJ.ajax({"data": data.field, "url":"index/Inventory/getInventoryList"},function (data) {
                if (data.code == 1){
                   viewHtml(data.data);
                }else {
                    layer.alert(data.msg);
                }
            });
        });

        //监听批量删除
        form.on('submit(LAY-submit-2)', function (data) {
            var tab_ = $("#tab_ .layui-this").text();
            var list_ = [];
            if (tab_ == "SKU盘点") {
                if (checked_.length > 0) {
                    data.field.list = checked_;
                } else {
                    toolJ.error('请选择盘点单!');
                    return false;
                }
            }
            layer.confirm('你确定要删除这些数据？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                    $.each(table.cache['LAY-list'], function (index, item) {
                        var index_ = data.field.list.indexOf(item.id)
                        if (index_ == -1) {
                            list_.push(item);
                        }
                    });
                }
                layer.closeAll();
                tableView(list_);
                checked_ = [];
                table.render(null, 'LAY-list');
                table.render(null, 'LAY-list2');
                form.render(null, 'LAY-list-form');
            });
        });

        //监听生成盘点单
        form.on('submit(LAY-submit-3)', function (data) {
            var tab_ = $("#tab_ .layui-this").text();
            if (tab_ == "SKU盘点"){
                if (checked_.length > 0){
                    data.field.type = 1;
                    data.field.list = checked_;
                }else {
                    toolJ.error('请选择盘点单!');
                    return false;
                }
            }else {
                if (data.field.base_warehouse_id == "") {
                    toolJ.error("请选择仓库");
                    return false;
                }
                data.field.type = 2;
            }

            toolJ.ajax({"data": data.field, "url":"index/Inventory/saveInventory"},function (data) {
                if (data.code == 1){
                    window.location.href = "/#/Inventory/getList";
                }else {
                    layer.alert(data.msg,{icon:2});
                }
            });

        });


        /*监听下拉框 国家值改变*/
        form.on('select(base_country_id)', function(data){
            var obj = $("select[name='base_warehouse_id']");
            obj.empty();
            toolJ.ajax({"data": {"base_country_id":data.value}, "url":"index/Inventory/getWarehouseByCountry"},function (data) {
                if (data.code == 1){
                    /*动态给下拉框赋值*/
                    var selectHtmlWarehouse = '<option value="">请选择</option>';
                    var warehouse = data.data;
                    for (var i in warehouse) {
                        selectHtmlWarehouse += '<option value="' + i + '">' + warehouse[i] + '</option>';
                    }
                    obj.append(selectHtmlWarehouse);
                    table.render(null, 'LAY-list');
                    form.render(null, 'LAY-list-form');
                }else {
                    layer.alert(data.msg,{icon:2});
                }
            });
        });

        /*监听复选框选中事件*/
        table.on('checkbox(LAY-list)', function(obj){
            if (obj.type == "all"){
                if (obj.checked){
                    checked_ = [];
                    if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                        $.each(table.cache['LAY-list'],function (index,item) {
                            checked_.push(item.id);
                        });
                    }
                }else {
                    checked_ = [];
                }
            }else {
                if (obj.checked){
                    checked_.push(obj.data.id);
                }else {
                    var index = checked_.indexOf(obj.data.id)
                    if (index > -1) {
                        checked_.splice(index, 1);
                    }
                }
            }
        });

        /*监听标签切换*/
        element.on('tab(test)', function(data){
//            console.log(this); //当前Tab标题所在的原始DOM元素
//            console.log(data.index); //得到当前Tab的所在下标
//            console.log(data.elem); //得到当前的Tab大容器
        });
    });

</script>
